From: Jan Beulich Date: Mon, 10 Mar 2014 10:03:53 +0000 (+0100) Subject: x86/HVM: fix memory type merging in epte_get_entry_emt() X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~5508 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https:/%22bookmarks://%22Dat/%22http:/www.example.com/cgi/%22https:/%22bookmarks:/%22Dat?a=commitdiff_plain;h=b99113b9d5fac5149de8496f55afa00e285b1ff3;p=xen.git x86/HVM: fix memory type merging in epte_get_entry_emt() Using the minimum numeric value of guest and host specified memory types is too simplistic - it works only correctly for a subset of types. It is in particular the WT/WP combination that needs conversion to UC if the two types conflict. Signed-off-by: Jan Beulich Reviewed-by: "Xu, Dongxiao" Acked-by: Keir Fraser --- diff --git a/xen/arch/x86/hvm/mtrr.c b/xen/arch/x86/hvm/mtrr.c index e7c0fd943f..b33bf34200 100644 --- a/xen/arch/x86/hvm/mtrr.c +++ b/xen/arch/x86/hvm/mtrr.c @@ -719,5 +719,35 @@ uint8_t epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn, MTRR_TYPE_WRBACK; hmtrr_mtype = get_mtrr_type(&mtrr_state, (mfn_x(mfn) << PAGE_SHIFT)); - return ((gmtrr_mtype <= hmtrr_mtype) ? gmtrr_mtype : hmtrr_mtype); + + /* If both types match we're fine. */ + if ( likely(gmtrr_mtype == hmtrr_mtype) ) + return hmtrr_mtype; + + /* If either type is UC, we have to go with that one. */ + if ( gmtrr_mtype == MTRR_TYPE_UNCACHABLE || + hmtrr_mtype == MTRR_TYPE_UNCACHABLE ) + return MTRR_TYPE_UNCACHABLE; + + /* If either type is WB, we have to go with the other one. */ + if ( gmtrr_mtype == MTRR_TYPE_WRBACK ) + return hmtrr_mtype; + if ( hmtrr_mtype == MTRR_TYPE_WRBACK ) + return gmtrr_mtype; + + /* + * At this point we have disagreeing WC, WT, or WP types. The only + * combination that can be cleanly resolved is WT:WP. The ones involving + * WC need to be converted to UC, both due to the memory ordering + * differences and because WC disallows reads to be cached (WT and WP + * permit this), while WT and WP require writes to go straight to memory + * (WC can buffer them). + */ + if ( (gmtrr_mtype == MTRR_TYPE_WRTHROUGH && + hmtrr_mtype == MTRR_TYPE_WRPROT) || + (gmtrr_mtype == MTRR_TYPE_WRPROT && + hmtrr_mtype == MTRR_TYPE_WRTHROUGH) ) + return MTRR_TYPE_WRPROT; + + return MTRR_TYPE_UNCACHABLE; }